]> git.saurik.com Git - bison.git/blame - data/c++.m4
style: fix comments
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
7d6bad19 5# Copyright (C) 2002-2013 Free Software Foundation, Inc.
cf147260 6
f16b0819 7# This program is free software: you can redistribute it and/or modify
cf147260 8# it under the terms of the GNU General Public License as published by
f16b0819 9# the Free Software Foundation, either version 3 of the License, or
cf147260 10# (at your option) any later version.
f16b0819 11#
cf147260
AD
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
f16b0819 16#
cf147260 17# You should have received a copy of the GNU General Public License
f16b0819 18# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 19
3953ed88
AD
20m4_include(b4_pkgdatadir/[c.m4])
21
38de4e57
AD
22# b4_comment(TEXT, [PREFIX])
23# --------------------------
24# Put TEXT in comment. Prefix all the output lines with PREFIX.
25m4_define([b4_comment],
26[b4_comment_([$1], [$2// ], [$2// ])])
27
28
cf147260
AD
29## ---------------- ##
30## Default values. ##
31## ---------------- ##
32
a4e25e1d 33b4_percent_define_default([[parser_class_name]], [[parser]])
7789b6e3
AD
34
35# Don't do that so that we remember whether we're using a user
36# request, or the default value.
37#
db8ab2be 38# b4_percent_define_default([[api.location.type]], [[location]])
7789b6e3 39
a4e25e1d 40b4_percent_define_default([[filename_type]], [[std::string]])
171ad99d 41b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
67501061 42
c1d19e10 43b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
44b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 46 [std::string], [[true]], [[false]])])
cf147260
AD
47
48
67501061 49
793fbca5
JD
50## ----------- ##
51## Namespace. ##
52## ----------- ##
53
67501061
AD
54m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
55
793fbca5 56
45eebca4
AD
57# Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
58# would compile as an absolute reference with 'parser' in the global namespace.
793fbca5
JD
59# b4_namespace_open would open an anonymous namespace and thus establish
60# internal linkage. This would compile. However, it's cryptic, and internal
61# linkage for the parser would be specified in all translation units that
62# include the header, which is always generated. If we ever need to permit
63# internal linkage somehow, surely we can find a cleaner approach.
64m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 65[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
66 [[namespace reference is empty]])])
67
68# Instead of assuming the C++ compiler will do it, Bison should reject any
69# invalid b4_namepsace_ref that would be converted to a valid
70# b4_namespace_open. The problem is that Bison doesn't always output
71# b4_namespace_ref to uncommented code but should reserve the ability to do so
72# in future releases without risking breaking any existing user grammars.
73# Specifically, don't allow empty names as b4_namespace_open would just convert
74# those into anonymous namespaces, and that might tempt some users.
75m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 76[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
77 [[namespace reference has consecutive "::"]])])
78m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 79[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
80 [[namespace reference has a trailing "::"]])])
81
82m4_define([b4_namespace_open],
67501061 83[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
84[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
87
88m4_define([b4_namespace_close],
67501061 89[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 90m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 92 [\1])),
b987342b 93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
94
95
e3990e3c
AD
96# b4_token_enums
97# --------------
c095d689 98# Output the definition of the tokens as enums.
cf147260 99m4_define([b4_token_enums],
e3990e3c
AD
100[[enum yytokentype
101 {
102 ]m4_join([,
103 ],
104 b4_symbol_map([b4_token_enum]))[
105 };]dnl
c095d689 106])
cf147260
AD
107
108
ef51bfa7
AD
109
110
cf147260
AD
111## ----------------- ##
112## Semantic Values. ##
113## ----------------- ##
114
7601a471
AD
115
116
117# b4_value_type_declare
118# ---------------------
b9e4eb5b 119# Declare semantic_type.
7601a471 120m4_define([b4_value_type_declare],
b9e4eb5b 121[ /// Symbol semantic values.
c5dbd909 122m4_ifdef([b4_union_members],
b9e4eb5b 123[ union semantic_type
d2e3c807 124 {
c5dbd909 125b4_user_union_members
b9e4eb5b
AD
126 };],
127[m4_if(b4_tag_seen_flag, 0,
128[[ typedef int semantic_type;]],
5458913a 129[[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
b9e4eb5b
AD
130
131
4f84717d
AD
132# b4_public_types_declare
133# -----------------------
134# Define the public types: token, semantic value, location, and so forth.
1d6b689b 135# Depending on %define token_lex, may be output in the header or source file.
4f84717d 136m4_define([b4_public_types_declare],
5458913a 137[[#ifndef ]b4_api_PREFIX[STYPE
7601a471 138]b4_value_type_declare[
4f84717d 139#else
5458913a 140 typedef ]b4_api_PREFIX[STYPE semantic_type;
4f84717d
AD
141#endif]b4_locations_if([
142 /// Symbol locations.
f6b561d9 143 typedef b4_percent_define_get([[api.location.type]],
7789b6e3 144 [[location]]) location_type;])[
1d6b689b 145
ff601366
AD
146 /// Syntax errors thrown from user actions.
147 struct syntax_error : std::runtime_error
148 {
149 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
150 location_type location;])[
151 };
152
4f84717d
AD
153 /// Tokens.
154 struct token
155 {
e3990e3c 156 ]b4_token_enums[
4f84717d 157 };
1d6b689b 158
26f95f5f 159 /// (External) token type, as returned by yylex.
4f84717d 160 typedef token::yytokentype token_type;
1d6b689b 161
26f95f5f
AD
162 /// Internal symbol number.
163 typedef int symbol_number_type;
164
165 /// Internal symbol number for tokens (subsumed by symbol_number_type).
166 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
167
1dbaf37f
TR
168 /// A complete symbol.
169 ///
170 /// Expects its Base type to provide access to the symbol type
171 /// via type_get().
172 ///
173 /// Provide access to semantic value]b4_locations_if([ and location])[.
174 template <typename Base>
175 struct basic_symbol : Base
1d6b689b 176 {
97ae878e
AD
177 /// Alias to Base.
178 typedef Base super_type;
179
1d6b689b 180 /// Default constructor.
b20e797a 181 basic_symbol ();
1d6b689b 182
1dbaf37f 183 /// Copy constructor.
b20e797a 184 basic_symbol (const basic_symbol& other);
ee9cf8c4
TR
185]b4_variant_if([[
186 /// Constructor for valueless symbols, and symbols from each type.
187]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
1dbaf37f 188 /// Constructor for valueless symbols.
7fc7df7a 189 basic_symbol (typename Base::kind_type t]b4_locations_if([,
ee9cf8c4 190 const location_type& l])[);]])[
1d6b689b 191
1dbaf37f 192 /// Constructor for symbols with semantic value.
7fc7df7a 193 basic_symbol (typename Base::kind_type t,
b20e797a
TR
194 const semantic_type& v]b4_locations_if([,
195 const location_type& l])[);
1d6b689b 196
97ae878e 197 ~basic_symbol ();
04816a6f 198
5f87211c 199 /// Destructive move, \a s is emptied into this.
b20e797a 200 void move (basic_symbol& s);
1d6b689b
AD
201
202 /// The semantic value.
203 semantic_type value;]b4_locations_if([
204
205 /// The location.
206 location_type location;])[
7d1aa2d6
AD
207
208 private:
209 /// Assignment operator.
210 basic_symbol& operator= (const basic_symbol& other);
1d6b689b
AD
211 };
212
1dbaf37f
TR
213 /// Type access provider for token (enum) based symbols.
214 struct by_type
1d6b689b 215 {
1d6b689b 216 /// Default constructor.
b20e797a 217 by_type ();
1d6b689b 218
1dbaf37f 219 /// Copy constructor.
b20e797a 220 by_type (const by_type& other);
1d6b689b 221
26f95f5f
AD
222 /// The symbol type as needed by the constructor.
223 typedef token_type kind_type;
1d6b689b 224
26f95f5f
AD
225 /// Constructor from (external) token numbers.
226 by_type (kind_type t);
97ae878e 227
26f95f5f
AD
228 /// Steal the symbol type from \a that.
229 void move (by_type& that);
1d6b689b 230
26f95f5f 231 /// The (internal) type number (corresponding to \a type).
97ae878e 232 /// -1 when this symbol is empty.
26f95f5f 233 symbol_number_type type_get () const;
1d6b689b 234
2b08bceb 235 /// The token.
b20e797a 236 token_type token () const;
1dbaf37f 237
675d9fe4
AD
238 enum { empty = 0 };
239
26f95f5f 240 /// The symbol type.
26f95f5f
AD
241 /// -1 when this symbol is empty.
242 token_number_type type;
1d6b689b 243 };
1dbaf37f
TR
244
245 /// "External" symbols: returned by the scanner.
246 typedef basic_symbol<by_type> symbol_type;
247
0623bacc 248]b4_symbol_constructor_declare])
4f84717d
AD
249
250
1d6b689b
AD
251# b4_public_types_define
252# ----------------------
253# Provide the implementation needed by the public types.
254m4_define([b4_public_types_define],
403febca 255[[ inline
ff601366
AD
256 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
257 : std::runtime_error (m)]b4_locations_if([
258 , location (l)])[
259 {}
260
1dbaf37f
TR
261 // basic_symbol.
262 template <typename Base>
b20e797a 263 inline
1dbaf37f
TR
264 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
265 : value ()
266 {}
04816a6f 267
1dbaf37f 268 template <typename Base>
b20e797a 269 inline
1dbaf37f
TR
270 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
271 : Base (other)
272 , value ()]b4_locations_if([
273 , location (other.location)])[
1d6b689b 274 {
1dbaf37f
TR
275 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
276 [other.value])],
277 [value = other.value;])[
1d6b689b
AD
278 }
279
1d6b689b 280
1dbaf37f 281 template <typename Base>
b20e797a 282 inline
1dbaf37f 283 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 284 [typename Base::kind_type t],
1dbaf37f
TR
285 [const semantic_type& v],
286 b4_locations_if([const location_type& l]))[)
287 : Base (t)
97ae878e 288 , value (]b4_variant_if([], [v])[)]b4_locations_if([
1dbaf37f 289 , location (l)])[
97ae878e 290 {]b4_variant_if([[
733fb7c5 291 (void) v;
97ae878e 292 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
1d6b689b 293
ee9cf8c4
TR
294]b4_variant_if([[
295 // Implementation of basic_symbol constructor for each type.
296]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
297 /// Constructor for valueless symbols.
1dbaf37f 298 template <typename Base>
b20e797a 299 inline
1dbaf37f 300 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
7fc7df7a 301 [typename Base::kind_type t],
1dbaf37f 302 b4_locations_if([const location_type& l]))[)
97ae878e
AD
303 : Base (t)
304 , value ()]b4_locations_if([
1dbaf37f 305 , location (l)])[
ee9cf8c4 306 {}]])[
1d6b689b 307
97ae878e
AD
308 template <typename Base>
309 inline
310 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
311 {]b4_variant_if([[
312 // User destructor.
26f95f5f 313 symbol_number_type yytype = this->type_get ();
97ae878e
AD
314 switch (yytype)
315 {
316]b4_symbol_foreach([b4_symbol_destructor])dnl
317[ default:
318 break;
319 }
320
321 // Type destructor.
322 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
323 }
324
1dbaf37f 325 template <typename Base>
b20e797a 326 inline
1dbaf37f
TR
327 void
328 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
1d6b689b 329 {
97ae878e
AD
330 super_type::move(s);
331 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
1dbaf37f 332 [s.value])],
97ae878e
AD
333 [value = s.value;])[]b4_locations_if([
334 location = s.location;])[
1d6b689b
AD
335 }
336
1dbaf37f 337 // by_type.
b20e797a 338 inline
1dbaf37f 339 ]b4_parser_class_name[::by_type::by_type ()
675d9fe4 340 : type (empty)
1dbaf37f 341 {}
1d6b689b 342
b20e797a 343 inline
1dbaf37f
TR
344 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
345 : type (other.type)
346 {}
1d6b689b 347
b20e797a 348 inline
1dbaf37f
TR
349 ]b4_parser_class_name[::by_type::by_type (token_type t)
350 : type (yytranslate_ (t))
351 {}
7be08dfb 352
97ae878e
AD
353 inline
354 void
355 ]b4_parser_class_name[::by_type::move (by_type& that)
356 {
357 type = that.type;
675d9fe4 358 that.type = empty;
97ae878e
AD
359 }
360
b20e797a 361 inline
1d6b689b 362 int
1dbaf37f 363 ]b4_parser_class_name[::by_type::type_get () const
1d6b689b
AD
364 {
365 return type;
366 }
e36ec1f4 367]b4_token_ctor_if([[
b20e797a 368 inline
1d6b689b 369 ]b4_parser_class_name[::token_type
1dbaf37f 370 ]b4_parser_class_name[::by_type::token () const
1d6b689b
AD
371 {
372 // YYTOKNUM[NUM] -- (External) token number corresponding to the
373 // (internal) symbol number NUM (which must be that of a token). */
374 static
375 const ]b4_int_type_for([b4_toknum])[
376 yytoken_number_[] =
377 {
378 ]b4_toknum[
379 };
380 return static_cast<token_type> (yytoken_number_[type]);
381 }
0623bacc
AD
382]])[]dnl
383b4_symbol_constructor_define])
384
385
386# b4_symbol_constructor_declare
387# b4_symbol_constructor_define
388# -----------------------------
389# Declare/define symbol constructors for all the value types.
390# Use at class-level. Redefined in variant.hh.
391m4_define([b4_symbol_constructor_declare], [])
392m4_define([b4_symbol_constructor_define], [])
393
1d6b689b 394
cbf25ce7
AD
395# b4_yytranslate_define
396# ---------------------
397# Define yytranslate_. Sometimes used in the header file,
398# sometimes in the cc file.
399m4_define([b4_yytranslate_define],
400[[ // Symbol number corresponding to token number t.
b20e797a 401 inline
cbf25ce7 402 ]b4_parser_class_name[::token_number_type
e36ec1f4 403 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
cbf25ce7
AD
404 [int])[ t)
405 {
406 static
407 const token_number_type
408 translate_table[] =
409 {
410]b4_translate[
411 };
412 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
413 const token_number_type undef_token_ = ]b4_undef_token_number[;
414
415 if (static_cast<int>(t) <= yyeof_)
416 return yyeof_;
417 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
418 return translate_table[t];
419 else
420 return undef_token_;
421 }
422]])
423
1d6b689b 424
cf147260
AD
425# b4_lhs_value([TYPE])
426# --------------------
427# Expansion of $<TYPE>$.
428m4_define([b4_lhs_value],
1fa5d8bb 429[b4_symbol_value([yyval], [$1])])
cf147260
AD
430
431
432# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
433# --------------------------------------
434# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
435# symbols on RHS.
436m4_define([b4_rhs_value],
1fa5d8bb
AD
437[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
438
cf147260
AD
439
440# b4_lhs_location()
441# -----------------
442# Expansion of @$.
443m4_define([b4_lhs_location],
444[(yyloc)])
445
446
447# b4_rhs_location(RULE-LENGTH, NUM)
448# ---------------------------------
449# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
450# on RHS.
451m4_define([b4_rhs_location],
d1ff7a7c 452[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
453
454
455# b4_parse_param_decl
456# -------------------
457# Extra formal arguments of the constructor.
458# Change the parameter names from "foo" into "foo_yyarg", so that
459# there is no collision bw the user chosen attribute name, and the
460# argument name in the constructor.
461m4_define([b4_parse_param_decl],
462[m4_ifset([b4_parse_param],
463 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
464
465m4_define([b4_parse_param_decl_1],
466[$1_yyarg])
467
468
469
470# b4_parse_param_cons
471# -------------------
472# Extra initialisations of the constructor.
473m4_define([b4_parse_param_cons],
474 [m4_ifset([b4_parse_param],
e9690142 475 [
cf147260
AD
476 b4_cc_constructor_calls(b4_parse_param)])])
477m4_define([b4_cc_constructor_calls],
e9690142 478 [m4_map_sep([b4_cc_constructor_call], [,
cf147260
AD
479 ], [$@])])
480m4_define([b4_cc_constructor_call],
e9690142 481 [$2 ($2_yyarg)])
cf147260
AD
482
483# b4_parse_param_vars
484# -------------------
485# Extra instance variables.
486m4_define([b4_parse_param_vars],
487 [m4_ifset([b4_parse_param],
e9690142 488 [
ac826bc4 489 // User arguments.
cf147260
AD
490b4_cc_var_decls(b4_parse_param)])])
491m4_define([b4_cc_var_decls],
e9690142 492 [m4_map_sep([b4_cc_var_decl], [
cf147260
AD
493], [$@])])
494m4_define([b4_cc_var_decl],
e9690142 495 [ $1;])
426903aa
AD
496
497
498## ---------##
499## Values. ##
500## ---------##
501
502# b4_yylloc_default_define
503# ------------------------
504# Define YYLLOC_DEFAULT.
505m4_define([b4_yylloc_default_define],
506[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
507 If N is 0, then set CURRENT to the empty location which ends
508 the previous symbol: RHS[0] (always defined). */
509
510# ifndef YYLLOC_DEFAULT
511# define YYLLOC_DEFAULT(Current, Rhs, N) \
512 do \
513 if (N) \
514 { \
515 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
516 (Current).end = YYRHSLOC (Rhs, N).end; \
517 } \
518 else \
519 { \
520 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
521 } \
522 while (/*CONSTCOND*/ false)
523# endif
524]])
1dce289a
AD
525
526## -------- ##
527## Checks. ##
528## -------- ##
529
530b4_token_ctor_if([b4_variant_if([],
531 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
532 [cannot use '%s' without '%s'],
533 [%define api.token.constructor],
534 [%define api.value.type variant]))])])